package nl.twleaks.alice.task;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Random;

import nl.twleaks.alice.entities.User;

@SuppressWarnings("serial")
public class BasicTask implements Serializable {
	private HashMap<String, Object> varMap;
	private int wait;
	
	protected User user;
	
	public BasicTask(User user) {
		wait = 0;
		varMap = new HashMap<String, Object>();
		
		this.user = user;
	}
	
	public final String get(String key) {
        try {
            return URLDecoder.decode((String) varMap.get(key), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
	
	public final void set(String key, Object value) {
        try {
            varMap.put(key, URLEncoder.encode((String) value, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
	
	public final boolean has(String key) {
		return varMap.containsKey(key);
	}

	public final int getWait() {
		return wait;
	}

	public final void setWait(int wait) {
		this.wait = wait;
	}
	
	public final void setWait(int min, int max) {
		Random random = new Random();
		this.wait = random.nextInt(max - min) + min;
	}

	public final User getUser() {
		return user;
	}
}
